package com.cake.liulanxiangzhu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cake.liulanxiangzhu.exception.ServiceException;
import com.cake.liulanxiangzhu.mapper.CakeProductMapper;
import com.cake.liulanxiangzhu.pojo.dto.AdminCakeskuDTO;
import com.cake.liulanxiangzhu.pojo.dto.AdminCakeskuInsertDTO;
import com.cake.liulanxiangzhu.pojo.dto.CartDTO;
import com.cake.liulanxiangzhu.pojo.entity.AdminCakesku;
import com.cake.liulanxiangzhu.pojo.entity.OmsOrderItem;
import com.cake.liulanxiangzhu.pojo.vo.AdminCakeskuVO;
import com.cake.liulanxiangzhu.pojo.vo.CakeProductVO;
import com.cake.liulanxiangzhu.service.AdminCakeskuService;
import com.cake.liulanxiangzhu.mapper.AdminCakeskuMapper;
import com.cake.liulanxiangzhu.web.JsonPage;
import com.cake.liulanxiangzhu.web.ServiceCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【admin_cakesku】的数据库操作Service实现
 * @createDate 2023-01-19 13:20:59
 */
@Service
public class AdminCakeskuServiceImpl extends ServiceImpl<AdminCakeskuMapper, AdminCakesku>
        implements AdminCakeskuService {
    @Autowired
    private AdminCakeskuMapper adminCakeskuMapper;
    @Autowired
    private CakeProductMapper cakeProductMapper;

    @Override
    public void addNew(AdminCakeskuInsertDTO adminCakeskuInsertDTO) {
        AdminCakesku adminCakesku = new AdminCakesku();
        BeanUtils.copyProperties(adminCakeskuInsertDTO, adminCakesku);
        adminCakesku.setCreatTime(new Date());
        adminCakesku.setUpdateTime(new Date());
        int insert = adminCakeskuMapper.insert(adminCakesku);
        if (insert != 1) {
            throw new ServiceException(ServiceCode.ERR_DELETE, "系统繁忙请稍后再试");
        }
    }

    @Override
    public void deleteById(Integer skuId) {
        AdminCakesku adminCakesku = super.getById(skuId);
        if (adminCakesku == null) {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "数据未找到");
        }
        boolean b = super.removeById(skuId);
        if (!b) {
            throw new ServiceException(ServiceCode.ERR_DELETE, "系统繁忙请稍后再试");
        }
    }

    @Override
    public void updateById(AdminCakeskuDTO adminCakeskuDTO) {
        AdminCakesku adminCakesku = new AdminCakesku();
        BeanUtils.copyProperties(adminCakeskuDTO, adminCakesku);
        adminCakesku.setUpdateTime(new Date());
        int i = adminCakeskuMapper.updateById(adminCakesku);
        if (i != 1) {
            throw new ServiceException(ServiceCode.ERR_DELETE, "系统繁忙请稍后再试");
        }

    }

    @Override
    public AdminCakeskuVO getSkuById(Integer skuId) {
        if (skuId == null) {
            throw new ServiceException(ServiceCode.ERR_DELETE, "未传入skuId");
        }
        AdminCakeskuVO vo = adminCakeskuMapper.queryAllBySkuId(skuId);
        return vo;
    }

    @Override
    public JsonPage<AdminCakeskuVO> getAllSku(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<AdminCakesku> adminCakeskus = adminCakeskuMapper.selectList(null);
        List<AdminCakeskuVO> vos = new ArrayList<>();
        for (AdminCakesku cakeskus : adminCakeskus) {
            AdminCakeskuVO vo = new AdminCakeskuVO();
            BeanUtils.copyProperties(cakeskus, vo);
            Integer cakeId = vo.getCakeId();
            CakeProductVO cakeProductVO = cakeProductMapper.getCakeById(cakeId);
            if (cakeProductVO != null) {
                vo.setCakeNm(cakeProductVO.getCakeName());
            }
            vos.add(vo);
        }
        return JsonPage.restPage(new PageInfo<>(vos));
    }

    @Override
    public List<AdminCakeskuVO> weiXinProductSku(Integer cakeId) {
        List<AdminCakeskuVO> adminCakeskuVOS = adminCakeskuMapper.queryAllByCakeId(cakeId);
        return adminCakeskuVOS;
    }

    @Override
    public List<AdminCakeskuVO> weiXinProductSkuList(CartDTO cartDTO) {
        List<OmsOrderItem> entities = cartDTO.getEntities();
        List<AdminCakeskuVO> vos = adminCakeskuMapper.queryAllByIds(entities);
        for (OmsOrderItem entity : entities) {
            for (AdminCakeskuVO vo : vos) {
                Integer quantity = entity.getQuantity();
                vo.setQuantity(quantity);
            }
        }
        return vos;
    }


}




